using System;
using System.Globalization;
using System.Runtime.InteropServices;
using PickGold.Charting;

namespace PickGold.Charting.Formulas
{
	internal class GeneralFormulas : PriceIndicators
	{
		public override void Formula(string formulaName, double[][] inputValues, out double[][] outputValues, string[] parameterList, string[] extraParameterList, out string[][] outLabels)
		{
			outputValues = null;
			outLabels = null;
			string name = formulaName.ToUpper(CultureInfo.InvariantCulture);
			try
			{
				string str2 = name;
				if (str2 == null)
				{
					goto Label_0048;
				}
				if (!(str2 == "RUNINGTOTAL"))
				{
					if (str2 == "RUNINGAVERAGE")
					{
						goto Label_003E;
					}
					goto Label_0048;
				}
				this.RuningTotal(inputValues, out outputValues);
				return;
			Label_003E:
				this.RunningAverage(inputValues, out outputValues);
				return;
			Label_0048:
				outputValues = null;
			}
			catch (IndexOutOfRangeException)
			{
				throw new InvalidOperationException(SR.ExceptionFormulaInvalidPeriod(name));
			}
			catch (OverflowException)
			{
				throw new InvalidOperationException(SR.ExceptionFormulaNotEnoughDataPoints(name));
			}
		}

		private void RuningTotal(double[][] inputValues, out double[][] outputValues)
		{
			if (inputValues.Length != 2)
			{
				throw new ArgumentException(SR.ExceptionPriceIndicatorsFormulaRequiresOneArray);
			}
			base.CheckNumOfValues(inputValues, 1);
			outputValues = new double[][] { new double[inputValues[0].Length], new double[inputValues[1].Length] };
			for (int i = 0; i < inputValues[0].Length; i++)
			{
				outputValues[0][i] = inputValues[0][i];
				if (i > 0)
				{
					outputValues[1][i] = inputValues[1][i] + outputValues[1][i - 1];
				}
				else
				{
					outputValues[1][i] = inputValues[1][i];
				}
			}
		}

		private void RunningAverage(double[][] inputValues, out double[][] outputValues)
		{
			if (inputValues.Length != 2)
			{
				throw new ArgumentException(SR.ExceptionPriceIndicatorsFormulaRequiresOneArray);
			}
			base.CheckNumOfValues(inputValues, 1);
			outputValues = new double[][] { new double[inputValues[0].Length], new double[inputValues[1].Length] };
			double num = 0.0;
			for (int i = 0; i < inputValues[0].Length; i++)
			{
				num += inputValues[1][i];
			}
			for (int j = 0; j < inputValues[0].Length; j++)
			{
				outputValues[0][j] = inputValues[0][j];
				if (j > 0)
				{
					outputValues[1][j] = ((inputValues[1][j] / num) * 100.0) + outputValues[1][j - 1];
				}
				else
				{
					outputValues[1][j] = (inputValues[1][j] / num) * 100.0;
				}
			}
		}

		public override string Name
		{
			get
			{
				return SR.FormulaNameGeneralFormulas;
			}
		}
	}
}

